package com.infoplatform.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.infoplatform.bean.SecondaryCommodity;
import com.infoplatform.bean.SecondarycommodityComment;
import com.infoplatform.bean.Student;
import com.infoplatform.service.SecondaryService;
import com.opensymphony.xwork2.ActionSupport;

public class SecondaryAction extends ActionSupport {
	
	private static final long serialVersionUID = 1L;
	private List<SecondaryCommodity> showCommodity;
	private List<Student> showCustomer;
	private Map<String,SecondarycommodityComment> searchComment;
	private Map<String,SecondaryCommodity> searchResult;
	private String secondaryTag = null;
	private SecondaryService secondaryService;
	private static final int BUFFER_SIZE = 16 * 1024;
	private File myPicture;
	private String contentType;

	private Integer commodityId;
	private Integer customerId;
	private String studentId;
	private String name;
	private Double price;
	private Integer attentionCount;
	private String detail;
	private String picture;
	private String tag;
	private String place;
	private String publicTime;
	
	public String SearchCommodityById()
	{
		showCommodity = secondaryService.SearchCommodityById(commodityId);
		return "secondaryDetail";
	}
	public String personalCommodity()
	{
		showCommodity = secondaryService.SearchCommodity(secondaryTag);
		return "personalSecondary";
	}
	public String personalCommodity_New()
	{
		studentId = "18";
		showCommodity = secondaryService.SearchCommodity_New(secondaryTag,Integer.parseInt(studentId));
		return "personalSecondary";
	}
	public String hispersonalCommodity_New()
	{
		studentId = "18";
		showCommodity = secondaryService.SearchCommodity_New(secondaryTag,Integer.parseInt(studentId));
		return "hispersonalSecondary";
	}
	public String SearchCommodity()
	{
		String resultPage = "";
		showCommodity = secondaryService.SearchCommodity(secondaryTag);
		if(secondaryTag.equals("sport"))
		{
			resultPage = "sportPage";
		}
		else if(secondaryTag.equals("digital"))
		{
			resultPage = "digitalPage";
		}
		else if(secondaryTag.equals("transport"))
		{
			resultPage = "transportPage";
		}
		else if(secondaryTag.equals("other"))
		{
			resultPage = "otherPage";
		}
		else if(secondaryTag.equals("book"))
		{
			resultPage = "bookPage";
		}
		return resultPage;
	}
	public String QueryCommodity()
	{
		showCommodity = secondaryService.QueryCommodity(secondaryTag);
		return "querySecondarySuccess";
	}
	private static void copy(File src, File dst) {
		try {
			InputStream in = null;
			OutputStream out = null;
			try {
				in = new BufferedInputStream(new FileInputStream(src),
						BUFFER_SIZE);
				out = new BufferedOutputStream(new FileOutputStream(dst),
						BUFFER_SIZE);
				byte[] buffer = new byte[BUFFER_SIZE];
				while (in.read(buffer) > 0) {
					out.write(buffer);
				}
			} finally {
				if (null != in) {
					in.close();
				}
				if (null != out) {
					out.close();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public String AddSecondary() throws ParseException
	{
		SecondaryCommodity second = createObject();
		if(myPicture != null)
		{
			String path = ServletActionContext.getServletContext().getRealPath("/images/second");
			String copy = "";
			String dataPath = "/InfoPlatform/images/second/";
			if(second.getTag().equals("book"))
			{
				copy = path + "\\book\\";
				dataPath += "book/";
			}
			else if(second.getTag().equals("sport"))
			{
				copy = path + "\\sport\\";
				dataPath += "sport/";
			}
			else if(second.getTag().equals("transport"))
			{
				copy = path + "\\transport\\";
				dataPath += "transport/";
			}
			else if(second.getTag().equals("digital"))
			{
				copy = path + "\\digital\\";
				dataPath += "digital/";
			}
			else if(second.getTag().equals("other"))
			{
				copy = path + "\\other\\";
				dataPath += "other/";
			}
			String time = new Date().getTime()+".jpg";
			copy += time;
			dataPath += time;
			second.setPicture(dataPath);
			second.setAttentionCount(0);
			File tempPicture = new File(copy);
			copy(myPicture,tempPicture);
		}
		
		secondaryService.AddSecondary(second);
		return "personalSecondary";
	}
	@SuppressWarnings("deprecation")
	public SecondaryCommodity createObject() throws ParseException
	{
		studentId = "18";
		SecondaryCommodity temp = new SecondaryCommodity();
		temp.setCommodityId(null);
		temp.setDetail(detail);
		temp.setName(name);
		temp.setPlace(place);
		temp.setStudentId(Integer.parseInt(studentId));
		temp.setTag(secondaryTag);
		temp.setPrice(price);
//		DateFormat format = new SimpleDateFormat("mm-dd-yyyy");
//		format.setLenient(false);
//		Timestamp ts = new Timestamp(format.parse(publicTime).getTime());
//		System.out.println(ts.toString());
//		temp.setPublicTime(ts);
		return temp;
	}
	public String SearchPage()
	{
		searchResult = secondaryService.SearchMap(secondaryTag);
		return "searchPageSuccess";
	}
	public String QueryCustomer()
	{
		showCustomer = secondaryService.QueryCustomer(customerId);
		return "queryCustomerSuccess";
	}
	public String DeleteCustomer()
	{
		showCustomer = secondaryService.DeleteCustomer(customerId);
		return "queryCustomerSuccess";
	}
	public String DeleteCommodity()
	{
		showCommodity = secondaryService.DeleteCommodityById(commodityId);
		return "querySecondarySuccess";
	}
	public void setShowCommodity(List<SecondaryCommodity> showCommodity) {
		this.showCommodity = showCommodity;
	}

	public List<SecondaryCommodity> getShowCommodity() {
		return showCommodity;
	}


	public String getSecondaryTag() {
		return secondaryTag;
	}


	public void setSecondaryTag(String secondaryTag) {
		this.secondaryTag = secondaryTag;
	}


	public SecondaryService getSecondaryService() {
		return secondaryService;
	}


	public void setSecondaryService(SecondaryService secondaryService) {
		this.secondaryService = secondaryService;
	}


	public static long getSerialversionuid() {
		return serialVersionUID;
	}


	public Integer getCommodityId() {
		return commodityId;
	}


	public void setCommodityId(Integer commodityId) {
		this.commodityId = commodityId;
	}


	public String getStudentId() {
		return studentId;
	}


	public void setStudentId(String studentId) {
		this.studentId = studentId;
	}


	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}


	public Double getPrice() {
		return price;
	}


	public void setPrice(Double price) {
		this.price = price;
	}


	public Integer getAttentionCount() {
		return attentionCount;
	}


	public void setAttentionCount(Integer attentionCount) {
		this.attentionCount = attentionCount;
	}


	public String getDetail() {
		return detail;
	}


	public void setDetail(String detail) {
		this.detail = detail;
	}


	public String getPicture() {
		return picture;
	}


	public void setPicture(String picture) {
		this.picture = picture;
	}


	public String getTag() {
		return tag;
	}


	public void setTag(String tag) {
		this.tag = tag;
	}


	public String getPlace() {
		return place;
	}


	public void setPlace(String place) {
		this.place = place;
	}


	public String getPublicTime() {
		return publicTime;
	}
	public void setPublicTime(String publicTime) {
		this.publicTime = publicTime;
	}
	public File getMyPicture() {
		return myPicture;
	}
	public void setMyPicture(File myPicture) {
		this.myPicture = myPicture;
	}
	public String getContentType() {
		return contentType;
	}
	public void setContentType(String contentType) {
		this.contentType = contentType;
	}
	public List<Student> getShowCustomer() {
		return showCustomer;
	}
	public void setShowCustomer(List<Student> showCustomer) {
		this.showCustomer = showCustomer;
	}
	public Integer getCustomerId() {
		return customerId;
	}
	public void setCustomerId(Integer customerId) {
		this.customerId = customerId;
	}
	public Map<String, SecondaryCommodity> getSearchResult() {
		return searchResult;
	}
	public void setSearchResult(Map<String, SecondaryCommodity> searchResult) {
		this.searchResult = searchResult;
	}
	public Map<String, SecondarycommodityComment> getSearchComment() {
		return searchComment;
	}
	public void setSearchComment(
			Map<String, SecondarycommodityComment> searchComment) {
		this.searchComment = searchComment;
	}
	
}
